﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;
//using Recon.Portal.Data;

namespace Recon.Portal.WebUI
{
    public partial class CustomerRemitterAccountMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((User)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!Page.IsPostBack)
            {
                //FillCustomerForMatching();
                //FillRemitter();
                BindCustomerAccountData();
                //lblCustmerName.Text = ((User)Session["CurrentUserVO"]).CustomerID + " - " + ((User)Session["CurrentUserVO"]).CustomerName;
                FillBank();
            }
        }

        private void BindCustomerAccountData()
        {
            CustomerEntity custEntity = new CustomerEntity();
            List<CustAccount> lstCustomer = null;
            try
            {
                lstCustomer = custEntity.GetCustomerAccountRecords(((User)Session["CurrentUserVO"]).CustomerID);
                if (lstCustomer != null)
                {
                    gvCustAccount.DataSource = lstCustomer;
                    gvCustAccount.DataBind();
                }
                else
                {
                    gvCustAccount.DataSource = null;
                    gvCustAccount.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (rbOption.SelectedValue == "C")
        //    {
        //        pnlCustomer.Visible = true;
        //        pnlCustomer.Enabled = true;
        //        pnlRemitter.Visible = false;
        //        pnlRemitter.Enabled = false;
        //    }
        //    else if (rbOption.SelectedValue == "R")
        //    {
        //        pnlCustomer.Visible = false;
        //        pnlCustomer.Enabled = false;
        //        pnlRemitter.Visible = true;
        //        pnlRemitter.Enabled = true;
        //    }
        //}

        //private void FillCustomerForMatching()
        //{
        //    CustomerEntity lstEntity = new CustomerEntity();
        //    List<Customer> dtCustomer = lstEntity.GetCustomer(string.Empty);

        //    if (dtCustomer != null && dtCustomer.Count<Customer>() > 0)
        //    {
        //        foreach (Customer customer in dtCustomer)
        //        {
        //            ddlCustomer.Items.Add(new ListItem(customer.CustomerID + " - " + customer.CustomerName, customer.CustomerID));
        //        }
        //    }
        //}

        //private void FillRemitter()
        //{
        //    RemitterEntity lstEntity = new RemitterEntity();
        //    List<Remitter> dtRemitter = lstEntity.GetRemitter(string.Empty);

        //    if (dtRemitter != null && dtRemitter.Count<Remitter>() > 0)
        //    {
        //        foreach (Remitter remitter in dtRemitter)
        //        {
        //            ddlRemitter.Items.Add(new ListItem(remitter.RemitterID + " - " + remitter.RemitterName, remitter.RemitterID));
        //        }
        //    }
        //}

        private void FillBank()
        {
            BankDAO bankDAO = DAOFactory.getBankDAO();
            List<Bank> bankLst = bankDAO.getAllBanks();
            if (bankLst != null && bankLst.Count > 0)
            {
                for (int i = 0; i < bankLst.Count; i++)
                {
                    ddlBankId.Items.Add(bankLst[i].BankID.Trim() + " - " + bankLst[i].BankName.Trim());
                    ddlBankId.Items[i + 1].Value = (bankLst[i].BankID.Trim());
                }
            }
        }

        private void ResetFields()
        {
            //rbOption.SelectedValue = "C";
            //ddlCustomer.SelectedValue = "0";
            //ddlRemitter.SelectedValue = "0";
            ddlBankId.SelectedValue = "0";
            txtAccountName.Text = "";
            txtAccountNumber.Text = "";
            btnSubmit.Text = "Add";
            //pnlCustomer.Visible = true;
            //pnlCustomer.Enabled = true;
            //pnlRemitter.Visible = false;
            //pnlRemitter.Enabled = false;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // DataTable dtCustomerDetails = new DataTable();
            //  db = new DBFactory();
            int retval = 0;
            CustAccount account = null;
            //if (rbOption.SelectedValue.Trim() == "C")
            //{
                //if (ddlCustomer.SelectedValue != "0")
                //{
                    //dtCustomerDetails = db.GetCustomerAccount(Convert.ToString(ddlCustomer.SelectedValue.Trim()), Convert.ToString(txtAccountNumber.Text.Trim()));
                    CustomerEntity custEntity = new CustomerEntity();
                    List<CustAccount> accountLst = custEntity.GetCustomerAccount(Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID), Convert.ToString(txtAccountNumber.Text.Trim()));
                    if (accountLst != null)
                    {
                        account = new CustAccount();
                        account.CustID = Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID);
                        account.AccountNumber = Convert.ToString(txtAccountNumber.Text.Trim());
                        account.AccountName = Convert.ToString(txtAccountName.Text.Trim());
                        account.BankID = Convert.ToString(ddlBankId.SelectedValue.Trim());
                        retval = custEntity.UpdateCustomerAccountRecords(account);

                        if (retval != 0)
                        {
                            lblMessage.Text = "Customer account modified successfully!";
                            btnClickPopup.Show();
                            ResetFields();
                        }
                        else
                        {
                            lblMessage.Text = "Error Modifying customer account detail!";
                            btnClickPopup.Show();
                        }
                    }
                    else
                    {
                        account = new CustAccount();
                        account.CustID = Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID);
                        account.AccountNumber = Convert.ToString(txtAccountNumber.Text.Trim());
                        account.AccountName = Convert.ToString(txtAccountName.Text.Trim());
                        account.BankID = Convert.ToString(ddlBankId.SelectedValue.Trim());
                        retval = custEntity.AddCustAccount(account);


                        if (retval != 0)
                        {
                            lblMessage.Text = "Customer account added successfully!";
                            btnClickPopup.Show();
                            ResetFields();
                        }
                        else
                        {
                            lblMessage.Text = "Error adding customer account detail!";
                            btnClickPopup.Show();
                        }
                    }
                    BindCustomerAccountData();

                //}
                //else
                //{
                //    lblMessage.Text = "Please select customer Id!";
                //    btnClickPopup.Show();
                //}
            //}
            //else
            //{
                /*if (ddlRemitter.SelectedValue != "0")
                {
                    RemitAccountDAO accountDAO = DAOFactory.GetRemitAccountDAO();
                    List<RemitAccount> accountLst = accountDAO.GetAllRemitAccounts(Convert.ToString(ddlRemitter.SelectedValue.Trim()), Convert.ToString(txtAccountNumber.Text.Trim()));
                    if (accountLst != null)
                    {
                        lblMessage.Text = "Account number for this customer already exist!";
                        btnClickPopup.Show();
                    }
                    else
                    {
                        RemitAccount account = new RemitAccount();

                        account.RemitID = Convert.ToString(ddlRemitter.SelectedValue).Trim();
                        account.AccountNumber = Convert.ToString(txtAccountNumber.Text.Trim());
                        account.AccountName = Convert.ToString(txtAccountName.Text.Trim());
                        account.Type = Convert.ToString(rbOption.SelectedValue.Trim());
                        account.BankID = Convert.ToString(ddlBankId.SelectedValue.Trim());

                        retval = accountDAO.Insert(account);
                        if (retval != 0)
                        {
                            lblMessage.Text = rbOption.SelectedItem.Text + " added successfully!";
                            btnClickPopup.Show();
                            ResetFields();
                        }

                        else
                        {
                            lblMessage.Text = "Error adding " + rbOption.SelectedItem.Text + " !";
                            btnClickPopup.Show();
                        }
                    }
                }
                else
                {
                    lblMessage.Text = "Please select remitter Id!";
                    btnClickPopup.Show();
                }*/


            //}


        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        //protected void gvCustAccount_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    gvCustAccount.EditIndex = e.NewEditIndex;
        //    BindCustomerAccountData();
        //}

        //protected void gvCustAccount_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        //{
        //    gvCustAccount.EditIndex = -1;
        //    BindCustomerAccountData();
        //}

        //protected void gvCustAccount_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
                
        //        //"@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        //        //cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        //    int retval = 0;
        //        try
        //        {
        //            CustomerEntity custEntity = new CustomerEntity();
        //            CustAccount account = new CustAccount();
        //            account.CustID = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[1].Text);
        //            account.AccountNumber = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[2].Text);
        //            account.AccountName = Convert.ToString(((TextBox)gvCustAccount.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
        //            account.BankID = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[4].Text);
        //            retval = custEntity.UpdateCustomerAccountRecords(account);
        //            if (retval != 0)
        //            {
        //                lblMessage.Text = "Customer account modified successfully!";
        //                btnClickPopup.Show();
        //            }
        //            else
        //            {
        //                lblMessage.Text = "Error in modify customer account detail!";
        //                btnClickPopup.Show();
        //            }
        //        }    
        //        catch
        //        {
                       
        //        }
        //        gvCustAccount.EditIndex = -1;
        //        BindCustomerAccountData();

        //}

        protected void gvCustAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
            int retval = 0;
            try
            {
                Label custId = ((Label)gvCustAccount.Rows[e.RowIndex].Cells[1].FindControl("lblCustomerID"));
                //LinkButton custId = ((LinkButton)gvCustAccount.Rows[e.RowIndex].Cells[0].FindControl("lnkSelect"));
                Label accNo = ((Label)gvCustAccount.Rows[e.RowIndex].Cells[2].FindControl("lblAccountNo"));
                CustomerEntity custEntity = new CustomerEntity();
                retval = custEntity.DeleteCustomerAccountRecords(custId.Text, accNo.Text);
                if (retval != 0)
                {
                    lblMessage.Text = "Customer account deleted successfully!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Error in deleting customer account detail!";
                    btnClickPopup.Show();
                }
            }
            catch
            {

            }
            BindCustomerAccountData();

        }

        //protected void gvCustAccount_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
        //    if (e.CommandName == "Select")
        //    {
        //        //GridViewRow row = ((e.CommandSource as CommandField).SelectText as GridViewRow);
        //        int i = Convert.ToInt32(e.CommandArgument);
        //        txtAccountNumber.Text = gvCustAccount.Rows[i].Cells[2].Text;
        //        txtAccountName.Text = gvCustAccount.Rows[i].Cells[3].Text;
        //        ddlBankId.Items.FindByValue(gvCustAccount.Rows[i].Cells[4].Text).Selected = true;

        //        //BindCustomerAccountData();
        //    }
        //}

        //protected void gvCustAccount_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
            //if (e.CommandName == "Select")
            //{
            //    //GridViewRow row = ((e.CommandSource as CommandField).SelectText as GridViewRow);
            //    int i = Convert.ToInt32(e.CommandArgument);
            //    txtAccountNumber.Text = gvCustAccount.Rows[i].Cells[2].Text;
            //    txtAccountName.Text = gvCustAccount.Rows[i].Cells[3].Text;
            //    ddlBankId.Items.FindByValue(gvCustAccount.Rows[i].Cells[4].Text).Selected = true;

            //    //BindCustomerAccountData();
            //}
        //}

       
        //protected void gvCustAccount_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    //GridViewRow row = ((e.CommandSource as CommandField).SelectText as GridViewRow);
        //    //int i = Convert.ToInt32(e.CommandArgument);
        //    txtAccountNumber.Text = gvCustAccount.SelectedRow.Cells[2].Text;
        //    txtAccountName.Text = gvCustAccount.SelectedRow.Cells[3].Text;
        //    ddlBankId.Items.FindByValue(gvCustAccount.SelectedRow.Cells[4].Text).Selected = true;

        //    //BindCustomerAccountData();
        //}
    }
}